package com.czy.owner.ui.setting;

import android.text.TextUtils;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.widget.MyEditText;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangyang on 2017/3/7.
 * 修改支付密码
 */
public class UpdatePaidPasswordActivity extends BaseActivity {
    @BindView(R.id.et_paid_old_psw)
    MyEditText etPaidOldPsw;
    @BindView(R.id.et_piad_new_psw)
    MyEditText etPaidNewPsw;
    @BindView(R.id.et_paid_psw_again)
    MyEditText etPaidPswAgain;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_paid_password;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
   getToolbarTitle().setText(R.string.update_paid_psw);
    }

    @Override
    protected void updateViews() {

    }


    @OnClick(R.id.btn_sure)
    public void sureOnClick(View view) {
        String oldPsw = etPaidOldPsw.getText().toString().trim();
        String newPsw = etPaidNewPsw.getText().toString().trim();
        String newPswAgain = etPaidPswAgain.getText().toString().trim();
        if (TextUtils.isEmpty(oldPsw)) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "旧的支付密码不能为空");
            etPaidOldPsw.requestFocus();
            return;
        }

        if (oldPsw.length() != 6) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "旧的支付密码必须是6位");
            etPaidOldPsw.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(newPsw)) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "新的支付密码不能为空");
            etPaidNewPsw.requestFocus();
            return;
        }

        if (newPsw.length() != 6) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "新的支付密码必须是6位");
            etPaidNewPsw.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(newPswAgain)) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "再次输入的密码不能为空");
            etPaidPswAgain.requestFocus();
            return;
        }

        if (newPswAgain.length() != 6) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "再次输入的密码必须是6位");
            etPaidPswAgain.requestFocus();
            return;
        }
        if (!newPsw.equals(newPswAgain)) {
            PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "两次输入的新密码不一致");
            etPaidPswAgain.requestFocus();
            return;
        }
        getUpdatePaidApi(oldPsw, newPsw);
    }

    private void getUpdatePaidApi(String oldPsw, String newPsw) {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_UPDATEPAYPASSWORD;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(UpdatePaidPasswordActivity.this).getSession());
        params.addBodyParameter("userId", UserHelper.getInstance().getUserInfoModel(UpdatePaidPasswordActivity.this).getUserId() + "");
        params.addBodyParameter("oldPassword", oldPsw);
        params.addBodyParameter("newPassword", newPsw);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess===" + result);
                String data = PhoneUtils.checkResponseFlag(UpdatePaidPasswordActivity.this, result);
                if (data != null) {
                    PhoneUtils.ShowToastMessage(UpdatePaidPasswordActivity.this, "支付密码修改成功");
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError===" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });

    }



}
